<template>
  <!-- 账户管理 -->
  <div class="account-container">
    <PageCard
      title="查询条件"
      :showTitle="false"
    >
      <el-form
        :model="formData"
        label-position="right"
        size="small"
        ref="searchForm"
        :inline="true"
        label-width="100px"
      >

        <el-form-item prop="name">
          <el-input
            class="w350"
            placeholder="请输入耗材名称"
            v-model="formData.name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSearch()"
          >查询</el-button>
          <el-button @click="handleReset()">重置</el-button>
        </el-form-item>
      </el-form>
    </PageCard>
    <PageCard
      title="耗材列表"
      icon_type="list"
      :hasCreate="true"
      btnText="新增耗材"
      @create="handleAdd()"
    >
      <template #btns>
        <el-button
          @click="handleImport"
          size="small"
        >导入</el-button>
      </template>
      <PageTable
        :data="data"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        @selectionChange="selectionChange"
        @handleChange="handleSearch"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="code"
          label="耗材编号"
          width="160"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          width="200"
          show-overflow-tooltip
        > </el-table-column>
        <el-table-column
          prop="model"
          label="型号"
        >
        </el-table-column>
        <el-table-column
          prop="format"
          label="规格"
        >
        </el-table-column>
        <el-table-column
          prop="price"
          label="单价"
        >
        </el-table-column>
        <el-table-column
          prop="factory"
          label="厂家"
        >
        </el-table-column>
        <el-table-column
          prop="brand"
          label="品牌"
        >
        </el-table-column>
        <el-table-column
          prop="reserve"
          label="库存数"
        >
        </el-table-column>
        <el-table-column
          prop="unit"
          label="单位"
        >
        </el-table-column>
        <el-table-column
          prop="reserveMax"
          label="最高库存"
        >
        </el-table-column>
        <el-table-column
          prop="reserveMin"
          label="最低库存"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              class="text-success"
              @click.stop="handleViewRecord(scope.row)"
            >出入库记录</el-button>
            <el-button
              type="text"
              @click.stop="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              @click="handleDel(scope.row)"
              class="text-danger"
            >删除</el-button>
            <!-- <el-popconfirm @onConfirm="handleDel(scope.row)" title="该耗材库存不为空或存在申请记录，是否确认删除！">
              <el-button type="text"
                class="text-danger ml10" slot="reference">删除</el-button>
            </el-popconfirm> -->
            <!-- <el-popover
              placement="top"
              width="160"
              v-model="visible"
            >
              <p>该耗材库存不为空或存在申请记录，是否确认删除！</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="mini"
                  type="text"
                  @click="visible = false"
                >取消</el-button>
                <el-button
                  type="primary"
                  size="mini"
                  @click.stop="handleDel(scope.row)"
                >确定</el-button>
              </div>
              <el-button
                type="text"
                class="text-danger"
                @click="visible = true"
                slot="reference"
              >删除</el-button>
            </el-popover> -->
          </template>
        </el-table-column>
      </PageTable>
    </PageCard>
    <Child
      ref="child"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></Child>
    <ImportFile
      ref="ImportFile"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></ImportFile>
    <StockRecord
      ref="StockRecord"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></StockRecord>
  </div>
</template>

<script>
import PageTable from "@/components/PageTable";
import PageCard from "@/components/PageCard";
import child from "./components/child.vue";
import importFile from "./components/importFile.vue";
import stockRecord from "./components/stockRecord.vue";
import * as consumableApi from "@/api/consumable.js";
import { settings } from "nprogress";
export default {
  name: "Users",
  components: {
    PageTable: PageTable,
    PageCard: PageCard,
    Child: child,
    ImportFile: importFile,
    StockRecord: stockRecord,
  },
  data() {
    return {
      visible: false,
      formData: {
        name: "",
      },
      currentPage: 1,
      pageSize: 10,
      total: 0,
      data: [],
      roleList: [], //角色列表
      multipleTable: [], //批量选择
    };
  },
  created() {
    this.handleSearch(1, 10);
  },
  methods: {
    // 查询
    handleSearch(current, limit) {
      this.currentPage = current;
      this.pageSize = limit;
      const params = {
        ...this.formData,
        pageIndex: current || 1,
        pageSize: limit || 10,
      };
      // 调接口查询
      this.handleGetDataList(params);
    },
    //获取列表数据
    handleGetDataList(params) {
      consumableApi.materialBaseList(params).then((res) => {
        if (res && res.success) {
          this.data = res.data;
          this.total = res.total;
        }
      });
    },
    // 重置
    handleReset() {
      this.$refs["searchForm"].resetFields();
    },
    handleAdd() {
      this.$refs.child.showModal("add");
    },
    handleEdit(row) {
      this.$refs.child.showModal("edit", row);
    },
    //出入库记录
    handleViewRecord(row) {
      this.$refs.StockRecord.showModal(row);
    },
    //导入
    handleImport() {
      this.$refs.ImportFile.showModal();
    },

    //删除用户
    handleDel(item) {
      consumableApi.judgeRemove(item.id).then((res) => {
        if (res && res.success) {
          if (res.data) {
            consumableApi.materialBaseRemove(item.id).then((res1) => {
              if (res1 && res1.success) {
                this.handleSearch(this.currentPage, this.pageSize);
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              }
            });
          } else {
            this.$confirm(res.message, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                consumableApi.materialBaseRemove(item.id).then((res1) => {
                  if (res1 && res1.success) {
                    this.handleSearch(this.currentPage, this.pageSize);
                    this.$message({
                      type: "success",
                      message: "删除成功!",
                    });
                  }
                });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消删除",
                });
              });
          }
        } else {
          this.$confirm("此操作将永久删除该耗材, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              consumableApi.materialBaseRemove(item.id).then((res1) => {
                if (res1 && res1.success) {
                  this.handleSearch(this.currentPage, this.pageSize);
                  this.$message({
                    type: "success",
                    message: "删除成功!",
                  });
                }
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
      });
      // consumableApi.materialBaseRemove(item.id).then((res) => {
      //   if (res && res.success) {
      //     this.handleSearch(this.currentPage, this.pageSize);
      //     this.visible = false;
      //     this.$message({
      //       type: "success",
      //       message: "删除成功!",
      //     });
      //   }
      // });

      return;
      this.$confirm("此操作将永久删除该耗材, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          consumableApi.materialBaseRemove(item.id).then((res) => {
            if (res && res.success) {
              this.handleSearch(this.currentPage, this.pageSize);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //多选
    selectionChange(val) {
      this.multipleTable = val;
    },
  },
};
</script>

<style lang="scss" scoped>
.account-container {
  .btns {
    text-align: right;
  }
}
</style>
